
import pymysql


class Mysql():

    def __init__(self):
        self.conn = pymysql.connect(host="localhost",user="root",password="root",database='school',charset='utf8')


    # 执行SQL,增删，改
    def exec_sql(self,sql):
        try:
            with self.conn.cursor() as cursor:
                cursor.execute(sql)
        except Exception as e:
            print(e)
        finally:
            self.conn.close()


    def get_all(self,sql):
        try:
            with self.conn.cursor() as cursor:
                cursor.execute(sql)
                result = cursor.fetchall()
                return result
        except Exception as e:
            print(e)
        finally:
            self.conn.close()



    def close(self):
        self.conn.close()